package com.dolphin.controller.admin;

import com.dolphin.base.BaseController;
import com.dolphin.commons.Constants;
import com.dolphin.commons.ResponseBean;
import com.dolphin.model.Article;
import com.dolphin.model.Permission;
import com.dolphin.permission.ApiAuth;
import com.dolphin.service.ArticleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 描述:
 * ${DESCRIPTION}
 *
 * @author 林平
 * @create 2022-09-13 22:15
 * 个人博客地址：https://www.nonelonely.com
 */
@Controller
@RequestMapping("/admin")
public class NoticeController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ArticleController.class);
    @Autowired
    private ArticleService articleService;

    @ApiAuth(name = "添加通知", permission = "admin:notice:addPage", type = Permission.ResType.NAV_LINK)
    @RequestMapping("/notice/addPage")
    public String addPage() {
        return view("static/admin/pages/notice/notice_create.html");
    }

    /**
     * 添加文章
     *
     * @return String
     */
    @PostMapping("/notice/add")
    @ResponseBody
    @ApiAuth(name = "添加通知", permission = "admin:notice:add")
    public ResponseBean add(@RequestBody Article article) {
        article.setUserId(getUser().getId());
        if (StringUtils.isBlank(article.getContent())) {
            return ResponseBean.fail("内容不允许为空", null);
        }
        if (articleService.add(article) > 0) {
            return ResponseBean.success("添加成功", article);
        }
        logger.error("通知添加失败: {}", article.toString());
        return ResponseBean.fail("添加失败", null);
    }

    @RequestMapping("/notice")
    @ApiAuth(name = "通知管理", permission = "admin:notice:index", groupId = Constants.ADMIN_MENU_GROUP_CONTENT)
    public String index(Model model) {
        return view("static/admin/pages/notice/notice_list.html");
    }

    @ApiAuth(name = "通知更新", permission = "admin:notice:updatePage", type = Permission.ResType.NAV_LINK)
    @RequestMapping("/notice/updatePage/{id}")
    public String updatePage(@PathVariable("id") String id, Model model) {
        Article article = articleService.getById(id);
        model.addAttribute("article", article);
        return view("/static/admin/pages/notice/notice_update.html");
    }

    /**
     * 更新文章
     *
     * @return String
     */
    @PostMapping("/notice/update")
    @ResponseBody
    @ApiAuth(name = "通知更新", permission = "admin:notice:update")
    public ResponseBean update(@RequestBody Article article) {
        if (StringUtils.isBlank(article.getContent())) {
            return ResponseBean.fail("内容不允许为空", null);
        }
        if (articleService.update(article) > 0) {
            return ResponseBean.success("更新成功", article);
        }
        logger.error("通知更新失败: {}", article.toString());
        return ResponseBean.fail("更新失败", null);
    }
}
